import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:inmotion_pro/pages/others/custom_view/setting_items/setting_row.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../common_enum/cell_setting_type.dart';
import '../../../models/settting_item_model.dart';

// 设置项分组组件
class SettingSection extends StatelessWidget {
  final String? title;
  final List<SettingRowModel> children;
  final EdgeInsetsGeometry? padding;
  // title在外层还是内层，默认外层
  final bool isTitleOutside;

  const SettingSection({
    super.key,
    required this.children,
    this.title,
    this.isTitleOutside = true,
    this.padding,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: padding ?? EdgeInsets.all(CONTENT_MARGIN),
      child: Visibility(
        visible: isTitleOutside,
        replacement: ClipRRect(
          borderRadius: BorderRadius.circular(12.w),
          child: Container(
            color: GrayColor.gray1,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                if (title != null && title!.isNotEmpty)
                  SizedBox(height: CONTENT_MARGIN),
                _title(),
                ..._buildSettingWidgets(children),
              ],
            ),
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _title(),
            ClipRRect(
              borderRadius: BorderRadius.circular(12.w),
              child: Container(
                color: GrayColor.gray1,
                child: Column(
                  children: _buildSettingWidgets(children),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 标题组件
  Widget _title() {
    if (title != null && title!.isNotEmpty) {
      return Padding(
        padding: EdgeInsets.only(left: CONTENT_MARGIN, bottom: CONTENT_MARGIN),
        child: LxText(
          title,
          fontWeightEnum: LxFontWeightEnum.titleLarge,
          color: Colors.white,
        ),
      );
    }
    return const SizedBox.shrink();
  }

  // 构建设置项组件列表
  List<Widget> _buildSettingWidgets(List<SettingRowModel> items) {
    return items.isNotEmpty
        ? items.map<Widget>((element) {
            final isLast = items.indexOf(element) == items.length - 1;
            switch (element.rowType) {
              case CellRowType.onlyTitle:
                return SettingRow.onlyTitle(
                  title: element.title,
                  leading: element.leading,
                  isLastRow: isLast,
                  isMainRow: element.isMainRow,
                  children: _buildSettingWidgets(element.subItems),
                );
              case CellRowType.onlyValue:
                return SettingRow.onlyValue(
                  title: element.title,
                  value: element.value,
                  leading: element.leading,
                  isLastRow: isLast,
                  isMainRow: element.isMainRow,
                  children: _buildSettingWidgets(element.subItems),
                );
              case CellRowType.onlyArrow:
                return SettingRow.onlyArrow(
                  onTap: () {
                    if (element.onTap != null) {
                      element.onTap!();
                    }
                  },
                  title: element.title,
                  leading: element.leading,
                  isLastRow: isLast,
                  isMainRow: element.isMainRow,
                  trailing: element.trailing,
                  children: _buildSettingWidgets(element.subItems),
                );
              case CellRowType.arrowWithValue:
                return SettingRow.arrowWithValue(
                  title: element.title,
                  value: element.value,
                  leading: element.leading,
                  trailing:
                      const Icon(Icons.chevron_right, color: Colors.white),
                  onTap: () {
                    if (element.onTap != null) {
                      element.onTap!();
                    }
                  },
                  isLastRow: isLast,
                  isMainRow: element.isMainRow,
                  children: _buildSettingWidgets(element.subItems),
                );
              case CellRowType.switchTile:
                return SettingRow.switchTile(
                  title: element.title,
                  leading: element.leading,
                  switchValue: element.switchValue ?? false,
                  switchOnChanged: (value) {
                    if (element.onChanged != null) {
                      element.onChanged!(value);
                    }
                  },
                  isLastRow: isLast,
                  isMainRow: element.isMainRow,
                  children: _buildSettingWidgets(element.subItems),
                );
            }
          }).toList()
        : [];
  }
}
